using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WbsEditor
{
    public partial class PreferencesDialog : Form
    {
        public PreferencesDialog()
        {
            InitializeComponent();
            _workPackagePrefix_TextChanged(null, null);
            //WbsSettings.RefreshAtLoadEnum refreshType = WbsSettings.RefreshAtLoadEnum.Ask;
            //try{ refreshType = (WbsSettings.RefreshAtLoadEnum)WbsEditor.Properties.Settings.Default.RefreshAtLoad; }
            //catch (Exception) { }
            _refreshAtLoadCombo.SelectedIndex = WbsEditor.Properties.Settings.Default.RefreshAtLoad;
        }

        public string FontFamily
        {
            get
            {
                return fontFamilyLabel.Font.FontFamily.Name;
            }

            set
            {
                fontFamilyLabel.Font = new Font(value, 8.25f);
                fontFamilyLabel.Text = value;
            }
        }

        private WbsTfsClient _tfsClient;
        public WbsTfsClient TfsClient
        {
            get
            {
                return _tfsClient;
            }
            set
            {
                _tfsClient = value;
            }
        }

        public WbsSettings.RefreshAtLoadEnum RefreshAtLoad
        {
            get
            {
                WbsSettings.RefreshAtLoadEnum refreshType = WbsSettings.RefreshAtLoadEnum.Ask;
                try
                {
                    refreshType = (WbsSettings.RefreshAtLoadEnum)_refreshAtLoadCombo.SelectedIndex;
                }
                catch (Exception)
                {
                }
                return refreshType;
            }
        }

        public Boolean EnableTfsTab
        {
            set
            {
                if (!value || string.IsNullOrEmpty(TfsClient.TeamProject))
                {
                    _tfsCustomFieldCombo.Enabled = false;
                    if (!string.IsNullOrEmpty(WbsEditor.Properties.Settings.Default.TfsCustomField))
                        _tfsCustomFieldCombo.Items.Add(WbsEditor.Properties.Settings.Default.TfsCustomField);
                    _tfsLeafWITypeCombo.Enabled = false;
                    _tfsLeafWITypeCombo.Items.Add(WbsEditor.Properties.Settings.Default.TfsLeafWIType);
                    _tfsRootWITypeCombo.Enabled = false;
                    _tfsRootWITypeCombo.Items.Add(WbsEditor.Properties.Settings.Default.TfsRootWIType);
                    _tfsLinkingCombo.Enabled = false;
                    _tfsFixLinkingCheck.Enabled = false;
                }
                else
                {
                    string[] workItemTypes = TfsClient.GetWorkItemTypes();
                    _tfsLeafWITypeCombo.Items.AddRange(workItemTypes);
                    _tfsRootWITypeCombo.Items.AddRange(workItemTypes);
                    string leafWIType = WbsEditor.Properties.Settings.Default.TfsLeafWIType;
                    if (!string.IsNullOrEmpty(leafWIType))
                    {
                        string[] fieldNames = TfsClient.GetFieldNames(leafWIType);
                        _tfsCustomFieldCombo.Items.AddRange(fieldNames);
                    }
                    else if (!string.IsNullOrEmpty(WbsEditor.Properties.Settings.Default.TfsCustomField))
                    {
                        _tfsCustomFieldCombo.Items.Add(WbsEditor.Properties.Settings.Default.TfsCustomField);
                    }
                }
            }
        }

        private void pickFontButton_Click(object sender, EventArgs e)
        {
            _fontSelector.Font = new Font(FontFamily, 8.25f);
            DialogResult res = _fontSelector.ShowDialog(this);
            if (DialogResult.OK == res)
            {
                FontFamily = _fontSelector.Font.FontFamily.Name;
            }
        }

        private void resetDisplayButton_Click(object sender, EventArgs e)
        {
            FontFamily = "Arial";
            WbsEditor.Properties.Settings.Default.ProgressColor = Color.LightGreen;
            WbsEditor.Properties.Settings.Default.InnerProgressColor = Color.Yellow;
            WbsEditor.Properties.Settings.Default.TitleColor = Color.Brown;
            WbsEditor.Properties.Settings.Default.PlausiMarkColor = Color.LightCoral;
            WbsEditor.Properties.Settings.Default.FindResultColor = Color.FromArgb(192, 192, 255);
        }

        private void _workPackagePrefix_TextChanged(object sender, EventArgs e)
        {
            string previewTitle = string.Format("TID: {0}2.3.1 - Sample Title", _workPackagePrefix.Text);
            _titlePreviewLabel.Text = previewTitle;
        }

        private void _tfsLeafWITypeCombo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string selectedCustomField = _tfsCustomFieldCombo.Text;
            _tfsCustomFieldCombo.Items.Clear();
            string wiType = _tfsLeafWITypeCombo.Text;
            if (string.IsNullOrEmpty(wiType))
                return;

            string[] fieldNames = TfsClient.GetFieldNames(wiType);
            _tfsCustomFieldCombo.Items.AddRange(fieldNames);

            if (_tfsCustomFieldCombo.Items.Contains(selectedCustomField))
            {
                _tfsCustomFieldCombo.Text = selectedCustomField;
            }
            else
            {
                MessageBox.Show("The previously selected custom field '" + selectedCustomField + "' does not exist in\n" +
                    "the selected leaf Work Item Type. It is reset to empty.", "Information", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
        }
    }
}